<?php
/**
 * Created by .
 * User: luogan
 * Date: 2021/12/28
 */

declare(strict_types=1);

namespace App\Service;

use App\Exception\ErrorException;
use App\Model\Menu;

class MenuService
{
    public function list($data)
    {
        $result = Menu::getMenuTreeList($data);
        if (!$result){
            throw new ErrorException();
        }
        return $result;
    }

    public function create($data)
    {
        $result = Menu::create($data);
        if (!$result){
            throw new ErrorException();
        }
        admin_log('新增菜单');
        return $result;
    }

    public function update($data)
    {
        $result = Menu::query()->where('id',$data['id'])->update($data);
        if (!$result){
            throw new ErrorException();
        }
        admin_log('更新菜单');
        return $result;
    }

    public function detail($id)
    {
        $result = Menu::query()->find($id);
        if (!$result){
            throw new ErrorException();
        }
        return $result;
    }

    public function delete($id)
    {
        $result = Menu::destroy($id);
        if (!$result){
            throw new ErrorException();
        }
        admin_log('删除菜单');
        return $result;
    }
}
